﻿#ifndef UMESSAGE_H
#define UMESSAGE_H

#ifdef __cplusplus
   extern "C" {
#endif

#include <libxml/xmlstring.h>

typedef struct _Start_st
{
	unsigned char startcode;
    unsigned char ss_dev;
	unsigned short ss_type;
	unsigned short ss_len;	
}Start_st;

typedef enum
{
	MSG_DATA_START,         
	MSG_DATA_ADD,          
	MSG_DATA_DEL,           
	MSG_DATA_MOD,
	MSG_DATA_STOPSAVING,
	MSG_DATA_RELOAD,
	MSG_DATA_END
}umsg_data_t;

typedef enum
{
    msg_jstype_null,

    msg_jstype_normal,
    msg_jstype_node,
    msg_jstype_prop
}msg_jstype_t;

typedef enum
{
    msg_jstype_node_null,

    msg_jstype_node_name,
    msg_jstype_node_value  
}msg_jstype_node_t;

typedef enum
{
    msg_jstype_prop_null,

    msg_jstype_prop_pair     
}msg_jstype_prop_t;


typedef enum
{
    msg_jstype_value_number,

    msg_jstype_value_string      
}msg_jstype_value_t;

typedef void (*umsg_cb)(unsigned short , char * );


#define USOCK_START_CODE 0x03
#define USOCK_END_CODE 0x0A


#define UMSG_SET_JSON_NODE(...)								        msg_jstype_node, ##__VA_ARGS__
#define UMSG_SET_JSON_NODE_NAME(STR_PAIR)				            msg_jstype_node_name, STR_PAIR
#define UMSG_SET_JSON_NODE_VALUE(STR_PAIR)			                msg_jstype_node_value, STR_PAIR
#define UMSG_SET_JSON_NODE_NULL()							        msg_jstype_node_null

#define UMSG_SET_JSON_PROP(...)								        msg_jstype_prop, ##__VA_ARGS__
#define UMSG_SET_JSON_PROP_PAIR(STR_PAIR1, STR_PAIR2)	            msg_jstype_prop_pair, STR_PAIR1, STR_PAIR2
#define UMSG_SET_JSON_PROP_NULL()							        msg_jstype_prop_null

#define UMSG_SET_JSON_NORMAL(STR_PAIR)		                        msg_jstype_normal, STR_PAIR

#define UMSG_SET_JSON_NULL()							            msg_jstype_null

#define UMSG_SET_JSON_NUMBER(NAME_STR,VALUE_STR)                    msg_jstype_value_number, (const xmlChar*)NAME_STR, (double)VALUE_STR
#define UMSG_SET_JSON_STRING(NAME_STR,VALUE_STR)                    msg_jstype_value_string, (const xmlChar*)NAME_STR, (const xmlChar*)VALUE_STR


void umsg_parse_json_add(unsigned short type, char *pbuf, umsg_cb cb);
void umsg_parse_json_delete(unsigned short type, char *pbuf, umsg_cb cb);
void umsg_parse_json_modify(unsigned short type, char *pbuf, umsg_cb cb);
int umsg_pack_json (char **ppbuf, ...);
char *umsg_pack_msg(char *pbuf, unsigned short len, unsigned char dev, unsigned short type);
void umsg_parse_json_handle(unsigned short type, char *pbuf);
void umsg_parse_json_handle2(unsigned short type, char *pbuf, umsg_cb cb);

#ifdef __cplusplus
	}
#endif

#endif	/*UMESSAGE_H*/
